/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RedeemPoint.java
 *
 * Created on 7-Apr-2010, 5:22:20 PM
 */

package ticketmaster.presentation.customer;

import java.sql.Timestamp;
import java.util.*;
import javax.swing.*;
import ticketmaster.businesslogic.*;
import ticketmaster.dao.*;
import ticketmaster.dataobject.*;
import ticketmaster.businessobject.*;
import ticketmaster.util.*;

/**
 *
 * @author Xiaolong Xue
 */
public class RedeemPoint extends CustomerBasePanel
{

    /** Creates new form RedeemPoint */
    public RedeemPoint()
    {
        Collection<Reward> rewards = DAOFactory.getInstance().getRewardDAO().getRewardsDO(new Reward());
        Collection<RewardBO> rewardBO = RewardController.getRewardBO(rewards);
        initComponents();
        tblGiftCard = new RewardTable(rewardBO);
        add(tblGiftCard, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, -1));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btnRedeem = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(RedeemPoint.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 25, -1, -1));

        btnRedeem.setFont(resourceMap.getFont("btnRedeem.font")); // NOI18N
        btnRedeem.setText(resourceMap.getString("btnRedeem.text")); // NOI18N
        btnRedeem.setName("btnRedeem"); // NOI18N
        btnRedeem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRedeemActionPerformed(evt);
            }
        });
        add(btnRedeem, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 490, 310, 40));
    }// </editor-fold>//GEN-END:initComponents

    private void btnRedeemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRedeemActionPerformed
        // TODO add your handling code here:
        redeemPoints();
        //show preimium myaccount panel
        PremiumCustomerPanel pcPanel = new PremiumCustomerPanel();
        getPnlParent().removeAll();
        getPnlParent().add(pcPanel);
        getPnlParent().validate();
        getPnlParent().repaint();
        pcPanel.setPnlParent(getPnlParent());
    }//GEN-LAST:event_btnRedeemActionPerformed

    private void redeemPoints()
    {
        JTable dataTable = tblGiftCard.getTable();
        for (int i = 0; i < dataTable.getRowCount(); ++i)
        {
            if (Boolean.parseBoolean(dataTable.getValueAt(i, 3).toString()))
            {
                String name = dataTable.getValueAt(i,0).toString();
                Reward reward = new Reward();
                reward.setName(name);
                DAOFactory.getInstance().getRewardDAO().findReward(reward);
               
                int points = reward.getPoints();
                int userID = getUser().getUserID();
                PremiumCustomer pc = new PremiumCustomer();
                pc.setCustomerID(userID);
                DAOFactory.getInstance().getPremiumCustomerDAO().findPremiumCustomer(pc);
                if(pc.getPoints() < points)
                {
                    JOptionPane.showMessageDialog(null, "Your points are not enough", "Redeem Points", JOptionPane.INFORMATION_MESSAGE);
                }
                else
                {
                    PremiumCustomerReward pcr = new PremiumCustomerReward();
                    pcr.setCustomerID(pc.getPremiumCustomerID());
                    pcr.setRewardID(reward.getRewardID());
                    pcr.setDate(DateTimeHelper.getCurrentDateTime());
                    DAOFactory.getInstance().getPremiumCustomerRewardDAO().insertPremiumCustomerReward(pcr);
                    pc.setPoints(pc.getPoints() - points);
                    DAOFactory.getInstance().getPremiumCustomerDAO().updatePremiumCustomer(pc);
                }
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRedeem;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
    private RewardTable tblGiftCard;
}
